package com.stone.controller.admin;

import com.stone.bean.AppResponse;
import com.stone.bean.dto.MenuAddDTO;
import com.stone.bean.dto.MenuUpdateDTO;
import com.stone.bean.vo.MenuNodeVO;
import com.stone.constant.Const;
import com.stone.entity.SysMenu;
import com.stone.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/sysMenu")
@Api(tags = "菜单管理")
public class SysMenuController {

    @Resource
    private SysMenuService sysMenuService;

    @PostMapping("/insert")
    @ApiOperation("保存菜单")
    public AppResponse addMenu(@RequestBody @Valid MenuAddDTO dto, @RequestHeader(value = Const.AUTH) String token) {
        dto.setToken(token);
        sysMenuService.addMenu(dto);
        return AppResponse.success();
    }

    @PostMapping("/update")
    @ApiOperation("修改菜单")
    public AppResponse updateMenu(@RequestBody @Valid MenuUpdateDTO dto, @RequestHeader(value = Const.AUTH) String token) {
        dto.setToken(token);
        sysMenuService.updateMenu(dto);
        return AppResponse.success();
    }

    @DeleteMapping("/delete/{menuId}")
    @ApiOperation("删除菜单")
    public AppResponse delMenu(@PathVariable(value = "menuId") Long menuId, @RequestHeader(value = Const.AUTH) String token) {
        sysMenuService.delMenu(menuId, token);
        return AppResponse.success();
    }

    @GetMapping("/active/{menuId}")
    @ApiOperation("修改菜单状态")
    public AppResponse changeEnable(@PathVariable(value = "menuId") Long menuId, @RequestHeader(value = Const.AUTH) String token) {
        sysMenuService.changeEnable(menuId, token);
        return AppResponse.success();
    }

    @GetMapping("/info/{menuId}")
    @ApiOperation("根据菜单Id获取菜单信息")
    public AppResponse<SysMenu> selectMenuById(@PathVariable(value = "menuId") Long menuId) {
        SysMenu sysMenu = sysMenuService.getById(menuId);
        return AppResponse.success().setRecord(sysMenu);
    }

    @GetMapping("/tree")
    @ApiOperation("获取菜单树")
    public AppResponse<List<MenuNodeVO>> selectTreeAll() {
        List<MenuNodeVO> list = sysMenuService.selectTreeAll();
        return AppResponse.success().setRecords(list);
    }


}
